﻿/*
Copyright (c) 2013, Marc Tanti
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the www.marctanti.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MARIA.level.situation;
using MARIA.level;
using MARIA.level.robot;

namespace MARIA
{
    public partial class SituationControl : UserControl
    {

        private Level level;

        /// <summary>
        /// An event which fires when a situation ends.
        /// </summary>
        public event MARIA.level.situation.Situation.SituationEndedHandler OnSituationEnded;

        public SituationControl()
        {
            InitializeComponent();
        }

        public void SetLevel(Level level)
        {
            this.level = level;
            lstSituationList.Items.Clear();
            foreach (Situation situation in level.Situations)
            {
                lstSituationList.Items.Add(situation.Name);
                situation.OnSituationEnded -= new Situation.SituationEndedHandler(situation_OnSituationEnded);
                situation.OnSituationEnded += new Situation.SituationEndedHandler(situation_OnSituationEnded);
                situation.ResidingRobot.OnRobotMoved -= new Robot.RobotMovedHandler(ResidingRobot_OnRobotMoved);
                situation.ResidingRobot.OnRobotMoved += new Robot.RobotMovedHandler(ResidingRobot_OnRobotMoved);
            }
            SetSelectedIndex(0);
        }

        void ResidingRobot_OnRobotMoved(object sender, RobotMovedEventArgs args)
        {
            txtMovementsLeft.Text = GetSelectedSituation().MovementsLeft.ToString();
            RefreshSituationPic();
        }

        void situation_OnSituationEnded(object sender, SituationEndedEventArgs args)
        {
            OnSituationEnded(sender, args);
        }

        public void ReinitializeSituations()
        {
            level.ReinitializeAllSituations();
        }

        public Robot GetRobot()
        {
            return level.Situations[GetSelectedIndex()].ResidingRobot;
        }

        public Situation GetSelectedSituation()
        {
            return level.Situations[GetSelectedIndex()];
        }

        public int GetSelectedIndex()
        {
            return lstSituationList.SelectedIndex;
        }

        public void SetSelectedIndex(int index)
        {
            lstSituationList.SelectedIndex = index;
        }

        public void SetSelectedSituation(Situation situation)
        {
            SetSelectedIndex(Array.IndexOf(level.Situations, situation));
        }

        public int GetSituationCount()
        {
            return level.Situations.Length;
        }

        public void MakeUneditable()
        {
            lstSituationList.Enabled = false;
        }

        public void MakeEditable()
        {
            lstSituationList.Enabled = true;
        }

        public void RefreshSituationPic()
        {
            Situation currSituation = GetSelectedSituation();
            picCurrSituation.Image = GetSituationPic(currSituation);
            txtMovementsLeft.Text = currSituation.MovementsLeft.ToString();
        }

        private void lstSituationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshSituationPic();
        }

        private const int gridSqSz = 29;
        private Image GetSituationPic(Situation situation)
        {
            Bitmap grid = new Bitmap(Properties.Resources.grid);
            Graphics g = Graphics.FromImage(grid);

            for (int y = 0; y < situation.Map.GetLength(0); y++)
            {
                for (int x = 0; x < situation.Map.GetLength(1); x++)
                {
                    g.DrawImage(situation.Map[y, x].SquareImage, x * gridSqSz + 1, y * gridSqSz + 1);
                }
            }
            g.DrawImage(situation.ResidingRobot.RobotImage, situation.ResidingRobot.CurrentLocation.X * gridSqSz + 1, situation.ResidingRobot.CurrentLocation.Y * gridSqSz + 1);

            return grid;
        }

    }
}
